﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using DevScope.CartaoDeCidadao;

namespace DemoCartaoCidadao
{
  public partial class frmSettings : Form
  {
    public delegate void UpdateSettingsHandler(object sender, UpdateSettingsEventArgs e);
    public event UpdateSettingsHandler UpdateSettings;

    public frmSettings()
    {
      InitializeComponent();
    }

    public frmSettings(List<string> cardReaders, string selectedReader, ProxyInfo proxy): this()
    {
      foreach (string cardReader in cardReaders)
        ddlCardReaders.Items.Add(cardReader);

      ddlCardReaders.Text = selectedReader;

      chkUseProxy.Checked = !String.IsNullOrEmpty(proxy.Server);
      txtAddress.Text = proxy.Server;
      if (proxy.Port != 0)
        udPort.Value = proxy.Port;
      chkUseProxy_CheckedChanged(null, null);

      chkUseAuthentication.Checked = !String.IsNullOrEmpty(proxy.Username);
      txtUsername.Text = proxy.Username;
      txtPassword.Text = proxy.Password;
      chkUseAuthentication_CheckedChanged(null, null);
    }

    private void chkUseProxy_CheckedChanged(object sender, EventArgs e)
    {
      lblAddress.Enabled = chkUseProxy.Checked;
      txtAddress.Enabled = chkUseProxy.Checked;
      lblPort.Enabled = chkUseProxy.Checked;
      udPort.Enabled = chkUseProxy.Checked;
      grpAuthentication.Enabled = chkUseProxy.Checked;
    }

    private void chkUseAuthentication_CheckedChanged(object sender, EventArgs e)
    {
      lblUsername.Enabled = chkUseAuthentication.Checked;
      txtUsername.Enabled = chkUseAuthentication.Checked;
      lblPassword.Enabled = chkUseAuthentication.Checked;
      txtPassword.Enabled = chkUseAuthentication.Checked;
    }

    private void btnClose_Click(object sender, EventArgs e)
    {
      ProxyInfo proxyInfo = new ProxyInfo();
      if (chkUseProxy.Checked)
      {
        proxyInfo.Server = txtAddress.Text;
        proxyInfo.Port = Convert.ToUInt32(udPort.Value);
        if (chkUseAuthentication.Checked)
        {
          proxyInfo.Username = txtUsername.Text;
          proxyInfo.Password = txtPassword.Text;
        }
      }

      UpdateSettingsEventArgs settings = new UpdateSettingsEventArgs(ddlCardReaders.Text, proxyInfo);
      this.UpdateSettings(this, settings);
      this.Close();
    }
  }

  public class UpdateSettingsEventArgs : System.EventArgs
  {
    private string selectedReader;
    private ProxyInfo proxyInfo;

    public string SelectedReader
    {
      get { return this.selectedReader; }
      set { ; }
    }

    public ProxyInfo ProxyConfiguration
    {
      get { return this.proxyInfo; }
      set { ; }
    }

    public UpdateSettingsEventArgs(string selectedReader, ProxyInfo proxyInfo)
    {
      this.selectedReader = selectedReader;
      this.proxyInfo = proxyInfo;
    }
  }
}
